package qlsl.androiddesign.http.service.commonservice;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.commonentity.User;
import qlsl.androiddesign.entity.otherentity.PoemDynamic;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 搜索模块 <br/>
 * 
 */
public class SearchService extends BaseService {

	private static String className = getClassName(SearchService.class);

	public static void searchGroup(final String keyword, final int type, final int page,
			final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "searchGroup");
		new HandlerThread(className, "searchGroup") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("group_search").addParam("keyword", keyword)
							.addParam("type", type).addParam("page", page).addParam("size", SoftwareConstant.PAGER_SIZE)
							.get();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Group> list = JSONArray.parseArray(jo.getString("data"), Group.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	public static void search(final String key, final int page, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在搜索");
		final HttpHandler handler = getHandler(functionView, listener, className, "search");
		new HandlerThread(className, "search") {
			public void run() {
				try {
					if (key.equals("测试")) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						List<PoemDynamic> list = new ArrayList<PoemDynamic>();
						String[] typeNames = new String[] { "精选原创", "官方发布" };
						List<String> tags = new ArrayList<String>();
						tags.add("标签1");
						tags.add("标签2");

						for (int index = (15 * page) + 1; index <= 15 * (page + 1); index++) {
							PoemDynamic item = new PoemDynamic();
							User user = new User();
							user.setAvatar(
									"http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/23f69832408d2333808d5e09e49a190d.jpg");
							user.setLevel(index * 3);
							user.setName("用户" + index);
							item.setUser(user);
							item.setPetUrl(
									"http://bmob-cdn-690.b0.upaiyun.com/2016/04/28/1e9673de407f5ef980f0478ebee24c79.jpg");
							item.setCommentNum(index * 10);
							item.setStoreNum(index * 5);
							item.setContent("诗篇" + index + "是一首慷慨激昂的诗词");
							item.setTypeName(index % 2 == 0 ? typeNames[0] : typeNames[1]);
							item.setTags(tags);
							list.add(item);
						}

						Pager pager = PagerUtils.createPager(page, 105);

						map.put("list", list);
						map.put("pager", pager);
						map.put("key", key);

						handler.sendMessage(WhatConstant.WHAT_NET_DATA_SUCCESS, map);
					} else {
						handler.sendMessage(WhatConstant.WHAT_NET_DATA_FAIL, "暂无搜索结果");
					}
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}
}
